var app = getApp();
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 请求到数据后，格式为 inTheaters: { movies: {xxxxxx} }
    inTheaters: {},
    comingSoon: {},
    top250: {},
    searchResult: {},
    searchPannelShow: false, // 是否展示搜索结果页面
    isEmpty: true,
    totalCount: 0,
    requestUrl: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var inTheatersUrl = app.globalData.g_bageUrl +
      "/v2/movie/in_theaters" + "?start=0&count=3&city=上海";
    var comingSoonUrl = app.globalData.g_bageUrl +
      "/v2/movie/coming_soon" + "?start=0&count=3";
    var top250Url = app.globalData.g_bageUrl +
      "/v2/movie/top250" + "?start=0&count=3";

    this.getMovieListData(inTheatersUrl, "inTheaters");
    this.getMovieListData(comingSoonUrl, "comingSoon");
    this.getMovieListData(top250Url, "top250");
  },
  /**
   * 获取电影信息
   */
  getMovieListData(url, setKey) {
    var _self = this;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'json'
      },
      success: function (res) {

        _self.processDoubanData(res.data, setKey);
      },
      fail: function () {

      }
    })
  },
  /**
   * 处理豆瓣传送过来的数据
   */
  processDoubanData(movieData, setKey) {
    var movies = [];
    var movieType = movieData.title;
    for (var sub in movieData.subjects) {
      var ele = movieData.subjects[sub];
      var title = ele.title;
      if (title.length >= 6) {
        title = title.substring(0, 6) + '...';
      }
      var temp = {
        stars: util.converStarsArr(ele.rating.stars),
        title: title,
        average: ele.rating.average,
        coverImagePath: ele.images.large,
        movieId: ele.id,
        movieType: movieType
      }
      movies.push(temp);
    }
    var readyData = {};
    if (this.data.searchPannelShow) { // 搜索页面处理数据
      if(this.data.isEmpty) {
        readyData[setKey] = {
          movies: movies
        };
        this.setData(readyData);
        this.setData({
          isEmpty: false
        });
      } else {
        var concatArr = this.data.searchResult.movies.concat(movies); // 累加的数据
        readyData[setKey] = {
          movies: concatArr
        };
        this.setData(readyData);
      }
    } else { // 电影页面
      
      readyData[setKey] = {
        movies: movies
      };
      this.setData(readyData); // 讲对应可以的值放进去，替换data属性里面相应的默认值
    }
    wx.hideLoading();
  },

  /**
   * 点击跳转到更多页面
   */
  onMoreTap(e) {
    var category = e.currentTarget.dataset.category;
    wx.navigateTo({
      url: 'more-movies/more-movies?category=' + category,
    })
  },

  /**
   * 点击进去详情页
   */
  onMovieTap(e) {
    var movieId = e.currentTarget.dataset.movieid;
    wx.navigateTo({
      url: 'movies-detail/movies-detail?movieId=' + movieId,
    })
  },

  /**
   * 监听搜索框激活
   */
  onBindFocus() {
    this.setData({
      searchPannelShow: true
    });
  },

  /**
   * 搜索框点击确定
   */
  onBindConfirm(e) {
    var text = e.detail.value;
    var searchUrl = app.globalData.g_bageUrl + "/v2/movie/search?q=" + text;
    this.setData({
      requestUrl: searchUrl
    });
    this.getMovieListData(searchUrl, "searchResult");
    wx.showLoading({
      title: '数据加载中...',
    });
  },

  /**
   * 关闭按钮
   */
  onClosePannel() {
    this.setData({
      searchPannelShow: false
    });
  },

  onReachBottom() {
    // 当搜索页面展现的时候，滑动的页面底部才去加载
    if (this.data.searchPannelShow) {
      var moreSerDataUrl = this.data.requestUrl + '&start=' + (this.data.totalCount + 20) + '&count=20';
      console.log(moreSerDataUrl)
      this.getMovieListData(moreSerDataUrl, "searchResult");
      this.setData({
        totalCount: this.data.totalCount + 20
      });
      wx.showLoading({
        title: '数据加载中...',
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})